//index.js
//获取应用实例
let App = getApp()

Page({
  // album存储自己的文章
  data: {
    newsWrApperList: [],
    value: '',
    active: 0,
    show: false,
    moyouquan: [],
    album: [],
    queryUserInfo: false,
    userInfo: {}
  },

  onPullDownRefresh: function () {
    console.log('onPullDownRefresh')

    if (this.data.active === 0) {
      let _this = this;
      let url = App.api_root + 'blog';  // 缺省加载新闻
      _this.setData({api_root: App.api_root})
      
      wx.request({
        url: url,
        header: {
          'content-type': 'Application/json'
        },
        method: 'GET',
        success (res) {
          let mydata = res.data;
          let status = mydata.status;

          if (res.statusCode === 200 && status === 200){
          
            let newsList = mydata.data;
            let newsWrApperList=[];
            for(let i = 0;i < newsList.length ; i++)
            {
              let news=newsList[i];
              let newsWrApper ={
                news:news
              }
              newsWrApperList.push(newsWrApper);
            }
            _this.setData({
              newsWrApperList: newsWrApperList
            });
          } else {
            console.log("新闻列表没有获取成功")
          }
          // console.log('newsWrApperList是：', _this.data.newsWrApperList);
          return true;
        },
        fail(res)
        {
          console.log('获取新闻失败');
          console.log(res);
          return false;
        }
      })
    } else {
      if (this.data.userInfo !== {}) {
        let _this = this
        App._getWithout('exhibition/list', {offset: 0, rows: 10}, result => {
          result = JSON.parse(result)
          console.log("这是加载exhibiton/list")
          console.log(result)
          _this.setData({moyouquan: result})
          // 筛选自己的文章, 根据name筛选
          let temp = []
          for (let i=0; i<this.data.moyouquan.length; i++) {
    
            if (this.data.moyouquan[i].name === this.data.userInfo.name) {
              temp.push(this.data.moyouquan[i])
            }
          }
          this.setData({album: temp})
        })
      } else {
        let _this = this
        App._getWithout('exhibition/list', {offset: 0, rows: 10}, result => {
          result = JSON.parse(result)
          _this.setData({moyouquan: result})
        })
      }
    }
  },

  selectActive (e) {
    let _this = this
    let active = e.detail.name // 拿到的是Number类型的
    _this.setData({active})
    

    if (active === 2) {  // 点击到相册
      if (App.checkIsLogin()) {
        // 已经登录
        // 已经登录第一次加载个人昵称
        if (!_this.data.queryUserInfo) {
          App._get('user_center', {openId: wx.getStorageSync('openId')}, result => {
            _this.setData({
              userInfo: result,
              queryUserInfo: true
            })
            console.log("个人信息如下")
            console.log(_this.data.userInfo)
          });
        }
        // 这里offset和rows先设置死了，后面再优化
        // 这时候墨友圈文章没有加载就加载，说明用户直接从新闻到相册，除了加载文章还要筛选文章
        if (_this.data.moyouquan.length === 0) {
          App._getWithout('exhibition/list', {offset: 0, rows: 10}, result => {
            result = JSON.parse(result)
            console.log("这是加载exhibiton/list")
            console.log(result)
            _this.setData({moyouquan: result})
            // 筛选自己的文章, 根据name筛选
            let temp = []
            for (let i=0; i<this.data.moyouquan.length; i++) {
      
              if (this.data.moyouquan[i].name === this.data.userInfo.name) {
                temp.push(this.data.moyouquan[i])
              }
            }
            _this.setData({album: temp})
            console.log("来自获取文章再筛选")
            console.log("album", _this.data.album)
          })
        } else {
          // 有文章
          // 如果album没有的话证明一是墨友圈加载了文章而再过来的要筛选文章
          // 二是可能已经来过相册但是自己没有文章也导致album.length === 0那无妨也筛选一起
          if (this.data.album.length === 0) {
            let temp = []
            for (let i=0; i<_this.data.moyouquan.length; i++) {
              if (this.data.moyouquan[i].name === this.data.userInfo.name) {
                temp.push(_this.data.moyouquan[i])
              }
            }
            this.setData({album: temp})
            console.log("temp", temp)
            this.setData({album: temp})
            console.log("来自下一个")
            console.log("album", this.data.album)
          }
        }
      } else {
        App.doLogin()  // 执行登录, 相册需要登录权限
      }
    }
    if (active === 1){
      // 这对应点击到了墨友圈active === 1
      // 这时候墨友圈文章没有加载就加载
      if (_this.data.moyouquan.length === 0) {
        App._getWithout('exhibition/list', {offset: 0, rows: 10}, result => {
          result = JSON.parse(result)
          console.log("这是加载exhibiton/list")
          console.log(result)
          _this.setData({moyouquan: result})
          // 不筛选自己的文章
        })
      } 
    }
  },

  showDetail(e)
  {
    let blogId = e.currentTarget.dataset.blogid;
     wx.navigateTo({
      url: './articles/index?blogId=' + blogId
    });
  },

  onLoad(){
    let _this = this;
    let url = App.api_root + 'blog';  // 缺省加载新闻
    _this.setData({api_root: App.api_root})
    
    wx.request({
      url: url,
      header: {
        'content-type': 'Application/json'
      },
      method: 'GET',
      success (res) {
        let mydata = res.data;
        let status = mydata.status;

        if (res.statusCode === 200 && status === 200){
         
          let newsList = mydata.data;
          let newsWrApperList=[];
          for(let i = 0;i < newsList.length ; i++)
          {
            let news=newsList[i];
            news.createTime = news.createTime.substring(0,9) + ' ' + news.createTime.substring(11, 18)
            let newsWrApper ={
              news:news
            }
            newsWrApperList.push(newsWrApper);
          }
          _this.setData({
            newsWrApperList: newsWrApperList
          });
        } else {
          console.log("新闻列表没有获取成功")
        }
        // console.log('newsWrApperList是：', _this.data.newsWrApperList);
        return true;
      },
      fail(res)
      {
        console.log('获取新闻失败');
        console.log(res);
        return false;
      }
    })
  },
})
